package com.commentlib.utils;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import androidx.annotation.NonNull;

import com.commentlib.app.BaseApplication;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by pc20170521 on 2017-09-08.
 */
public class FileUtils {

    private static FileUtils fileUtils = null;

    public static FileUtils getIntance(){
        if (fileUtils == null){
            synchronized (FileUtils.class){
                if (fileUtils == null){
                    fileUtils = new FileUtils();
                }
            }
        }
        return fileUtils;
    }

    public final static String dir = "/renli/";

    //Context.getExternalFilesDir() --> SDCard/Android/data/你的应用的包名/files/ 目录，一般放一些长时间保存的数据
    //Context.getExternalCacheDir() --> SDCard/Android/data/你的应用包名/cache/目录，一般存放临时缓存数据
    public final static String DIRS = Environment.getExternalStorageDirectory() + dir;
    public final static String DIR = BaseApplication.context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) + dir;
    /***保存图片路径*/
    public final static String savePic = DIR + "image/";

    public File createFolder(String path) {
        // 首先保存图片
        File appDir = new File(path);
        if (!appDir.exists()) {
            return appDir.mkdirs() ? appDir : null;
        }
        return appDir;
    }

    /**
     * 创建文件
     * @param path
     * @return
     */
    public File createFile(String path, String fliename) {
        // 首先保存图片
        File appDir = new File(path);
        if (!appDir.exists()) {
            appDir.mkdirs();
        }
        return new File(appDir, fliename);
    }

    /****获取文件***/
    public File getFlieByName(String filePath){
        File file = null;
        if (storegeAvailable()){
            file = new File(filePath);
        }
        return file;
    }

    /****获取文件列表****/
    public File[] getVideoFlieList(String filePath){
        File[] fileList = null;
        if (storegeAvailable()){
            try {
                File file = new File(filePath);
                if (file.exists()){
                    fileList = file.listFiles();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return fileList;
    }

    /*****
     * 判断內存是否可用
     * @return
     */
    public boolean storegeAvailable(){
        boolean isAvailable = false;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
            isAvailable = true;
        }
        return isAvailable;
    }

    /***
     * 判断文件是否存在
     * @param filePath
     * @return
     */
    public boolean isExists(String filePath){
        File file = new File(filePath);
        return file.exists();
    }

    /***删除单个文件**/
    public boolean delFile(final String filePath){
        final File file = new File(filePath);
        if (file == null || !file.exists() || file.isDirectory()) {
            return false;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                file.delete();
            }
        }).start();

        return true;
    }

    /***删除文件夹下的全部文件**/
    public void delAllFile(final File file){
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            }else if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (File f : files) {
                    delAllFile(f);
                }
            }
            file.delete();
        }
    }

    /***
     * 保存图片到相册
     */
    public boolean saveBitmapToPic(File file, Bitmap bmp){
        try {
            FileOutputStream fos = new FileOutputStream(file);
            //通过io流的方式来压缩保存图片
            boolean isSuccess = bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
            //把文件插入到系统图库
            //保存图片后发送广播通知更新数据库
            insertImageMedia(file);
            if (isSuccess) {
                return true;
            } else {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 把文件插入到系统图库
     * @param file
     * @throws FileNotFoundException
     */
    public static void insertImageMedia(@NonNull File file) throws FileNotFoundException {
        // 把文件插入到系统图库
        MediaStore.Images.Media.insertImage(BaseApplication.activity.getContentResolver(),
                file.getAbsolutePath(), file.getName(), null);

        // 通知图库更新
        BaseApplication.activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
    }
}
